package qs.openxt.libs.qrcode;

import android.content.Context;

import qs.openxt.libs.progress.Wait;

public class QrcodeTasking{
	public static final int RES_ERROR = -1;
	public static final int RES_SUCCESS = 0;
	public static final int RES_FAIL = 1;
	public static final int RES_NODATA = 2;
	protected Context context = null;
	
	private QrcodeTask task = null;
	private boolean isShowDialogProgress = true;
	
	protected OnScanningActionListener listener = null;
	public QrcodeTasking(Context context) {
		this.context = context;
	}
	
	public void setOnScanningActionListener(OnScanningActionListener l){
	    this.listener = l;
	}

	public void setShowDialogProgress(boolean isShowDialogProgress) {
		this.isShowDialogProgress = isShowDialogProgress;
	}

	public void doRequest(String path) {
		if(isShowDialogProgress)
			Wait.sharedInstance().start(context, "正在扫描二维码...，请稍候");
		
		try{
			task = new QrcodeTask(this);
			task.execute(path);
		}catch(Exception e){
			Wait.sharedInstance().stop();
		}
	}
	
	public void onSuccess(String result){
		if(isShowDialogProgress)
			Wait.sharedInstance().stop();
		
		if (listener != null) {
			listener.onScanningActionCompleted(result);
		}
	}

	public void onError(){
		if(isShowDialogProgress)
			Wait.sharedInstance().stop();
		
		if (listener != null) {
			listener.onScanningActionFailed();
		}
	}

	public abstract interface OnScanningActionListener {
		public abstract void onScanningActionCompleted(String result);

		public abstract void onScanningActionFailed();
	}
}
